package com.sdkx.app.datalayers;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.sdkx.app.model.WayPoint;
import com.sdkx.app.model.WayPointTypes;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import cn.creable.gridgis.display.Display;
import cn.creable.gridgis.display.MarkerSymbol;
import cn.creable.gridgis.geometry.IEnvelope;
import cn.creable.gridgis.geometry.Point;

public class WayPointsLayer extends MapDraw {
    private final static int POINTS_SHOW = 80;
    List<WayPoint> wayPoints = new ArrayList<>();
    List<WayPoint> allwayPoints = new ArrayList<>();
    Paint paint;

    public WayPointsLayer(Context context) {
        super(context);
        paint = new Paint();
        paint.setTextSize(18);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        String indexinfo = "create index if not exists points_mapshow on waypoint (type,latitude,longitude )";
        LitePal.getDatabase().execSQL(indexinfo);
    }

    @Override
    protected void draw2(Canvas canvas, Display display) {
        float zoom = display.getDisplayTransformation().getZoom();
        //  if(zoom <2.764279e-02f) {
        MarkerSymbol markerSymbol = new MarkerSymbol(24, 0, 0xFFFF0000, 0, 0);
        Canvas oldG = display.getCanvas();
        display.setCanvas(canvas);
        for (WayPoint point : wayPoints) {
            switch (point.getType()) {
                case 1:
                    markerSymbol.setColor(0xFFFF0000);
                    break;
                case 2:
                    markerSymbol.setColor(0xFF00FF00);
                    break;
                case 3:
                    markerSymbol.setColor(0xFF0000FF);
                    break;
                case 4:
                    markerSymbol.setColor(0xFF888888);
                    break;
            }

            display.DrawPoint(new Point(point.getDoubleLongitude(), point.getDoubleLatitude()), markerSymbol);
        }
        display.setCanvas(oldG);
//        }
    }

    @Override
    public void upData() {
        initPointType();
        if (allwayPoints.size() == 0)
            allwayPoints = getWayPoint();
        IEnvelope env = mapControl.getExtent();
        List<WayPoint> list = new ArrayList<>();
        double xMax = env.getXMax();
        double xMin = env.getXMin();
        double yMax = env.getYMax();
        double yMin = env.getYMin();
        double lon = 0;
        double lat = 0;
        for (WayPoint point : allwayPoints) {
            lat = point.getDoubleLatitude();
            lon = point.getDoubleLongitude();
            if (lat > yMin && lat < yMax && lon < xMax && lon > xMin) {
                list.add(point);
            }
        }
        int count = list.size();
        int div = count / POINTS_SHOW;
        if (div > 0) {
            List<WayPoint> templist = new ArrayList<>();
            for (int i = 0; i < count; i += div) {
                templist.add( list.get(i));
            }
            wayPoints = templist;
        } else {
            wayPoints = list;
        }

    }

    private List<WayPoint> getWayPoint() {
        List<WayPoint> list = new ArrayList<>();
        List<WayPointTypes> types = LitePal.where("show=1").find(WayPointTypes.class);
        if (types.size() > 0) {
            StringBuilder builder = new StringBuilder();
            builder.append("type in (");
            int count = 0;
            String po = ",";
            for (WayPointTypes wtype : types) {
                if (count == 0)
                    builder.append(wtype.getTypeid());
                else {
                    builder.append(po);
                    builder.append(wtype.getTypeid());
                }
                count++;
            }
            builder.append(")");
            list = LitePal.where(builder.toString()).find(WayPoint.class);
        }
        return list;
    }

    private void initPointType() {
        int count = LitePal.count(WayPointTypes.class);
        if (count == 0) {
            for (WayPoint.WayPointType e : WayPoint.WayPointType.values()) {
                WayPointTypes typeitem = new WayPointTypes();
                typeitem.setShow(1);
                typeitem.setTypeid(e.ordinal());
                String name = "";
                switch (e) {
                    case AIS_AIRPORT:
                        name = "民用机场";
                        break;
                    case MILITARY_AIRPORT:
                        name = "军用机场";
                        break;
                    case AM_AIRPORT:
                        name = "军民合用机场";
                        break;
                    case AIS_NDB:
                        name = "民用航点NDB";
                        break;
                    case AIS_VOR_DME:
                        name = "民用航点VOR,DME";
                        break;
                    case AIS_POINT:
                        name = "民用航点";
                        break;
                    case MILITARY_POINT:
                        name = "军用航点";
                        break;
                    case PERSON_AIRPORT:
                        name = "个人机场";
                        break;
                    case PERSON_POINT:
                        name = "个人航点";
                        break;
                    default:
                        continue;
                }
                typeitem.setName(name);
                typeitem.save();
            }
        }
    }
}
